/**
 * @author ashok
 * Chatter Notifier App.
 * 
 * Description: Controller class for chatter Notifier.
 * 
 * Copyright:  Copyright 2010
 * 
 * This file is part of Chatter Notifier. 
 * 
 * Chatter Notifier is free software: you can redistribute it and/or modify
 * it under the terms of the New BSD license.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
public class ChatterProfileController {
	private static Integer MEMBER_PER_ROW = 4; 
	public Chatter_Profile__c chatterProfile {get;set;}
	public String currentUserId {get; private set;}
	public User currentUser {get; private set;}
	public boolean showEditForm {get; private set;}
	public boolean showAnniversary {get; private set;}
	public List<EntitySubscription> following {get;set;}
	public List<EntitySubscription> followers {get;set;}
	public List<EntitySubscriptionWrapper> followingWrapper {get;set;}
	public List<EntitySubscriptionWrapper> followersWrapper {get;set;}
	public Integer followerCount {get;set;}
	public Integer followingCount {get;set;}
	public String birthDayString {get; private set;}
	public String anniversaryStr {get; private set;}
	public boolean showSettingForm {get; private set;}
	public boolean showEditLink {get; private set;}
	
	public Chatter_Profile__c dateCheck {get {
		return new Chatter_Profile__c(Anniversary__c = system.today().addYears(-10));
	} set;}
	
    public ChatterProfileController() {		
    }
    
    public void init() {
    	showEditLink = false;
    	showEditForm = false;
    	showSettingForm = false;
    	currentUserId = Apexpages.currentPage().getParameters().get('userId');
    	if(currentUserId == null || currentUserId == Userinfo.getUserId()) {
    		currentUserId = Userinfo.getUserId();
    		showEditLink = true;
    	}
    	currentUser = [Select Name, State, PostalCode, Phone, Email, Country, City From User where id =: currentUserId];
    	
    	loadChatterProfile();
    	
    	following = ChatterUtil.getFollowingUserList(currentUserId).values();
    	followingCount = following.size();
    	followingWrapper = createEntitySubscriptionWrapper(following);
    	
    	followers = ChatterUtil.getFollowers(currentUserId);
    	followerCount = followers.size();
    	followersWrapper = createEntitySubscriptionWrapper(followers);
    	
    	showAnniversary = isAnniversaryVisible();
    	
    	birthDayString = ChatterUtil.getFormatedBirthdayDate(chatterProfile.Birthday__c, chatterProfile.Birthday_display__c, ChatterUtil.DIPLAY_ON_PROFILE);
    	anniversaryStr = ChatterUtil.getFormatedBirthdayDate(chatterProfile.Anniversary__c, null, ChatterUtil.DIPLAY_ON_PROFILE);
    }
    
    public void loadChatterProfile() {
    	List<Chatter_Profile__c> chatterProfileList = null; 
    	chatterProfileList = [Select Id, Name, User__c, User__r.Name, Birthday__c, Sex__c, Relationship_Status__c,
    					  Anniversary__c, Birthday_display__c, Birthday_Notification__c, Anniversary_Notification__c,
    					  Event_Notification__c From Chatter_Profile__c where User__c =: currentUserId];

		if(chatterProfileList == null || chatterProfileList.isEmpty()) {
    		chatterProfile = new Chatter_Profile__c(Name = currentUser.Name, User__c = currentUserId, Birthday_Notification__c = false, Anniversary_Notification__c = false,
    					  Event_Notification__c = false);
    	} else {
    		chatterProfile = chatterProfileList.get(0);
    	}    					  
    }
    
    public void editChatterProfile() {
    	 showEditForm = true;
    	 loadChatterProfile();
    	 showAnniversary = isAnniversaryVisible();
    }    
    
    public void editNotificationSetting() {
    	 showSettingForm = true;
    }
    
    public void cancel() {
    	showEditForm = false;
    }
    
    public void cancelNotificationSetting() {
    	showSettingForm = false;
    }
    
    public void save() {
    	upsert chatterProfile;
    	chatterProfile = getchatterProfile(chatterProfile.id);
    	showEditForm = false;
    	showAnniversary = isAnniversaryVisible();
    	birthDayString = ChatterUtil.getFormatedBirthdayDate(chatterProfile.Birthday__c, chatterProfile.Birthday_display__c, ChatterUtil.DIPLAY_ON_PROFILE);
    	anniversaryStr = ChatterUtil.getFormatedBirthdayDate(chatterProfile.Anniversary__c, null, ChatterUtil.DIPLAY_ON_PROFILE);
    }
    
    public void saveSetting() {
    	upsert chatterProfile;
    	chatterProfile = getchatterProfile(chatterProfile.id);
    	showSettingForm = false;
    }
    
    private Chatter_Profile__c getchatterProfile(String chatterProfileId) {
    	Chatter_Profile__c cp  = [Select Id, Name, User__c, User__r.Name, Birthday__c, Sex__c, Relationship_Status__c,
	    					  Anniversary__c, Birthday_display__c, Birthday_Notification__c, Anniversary_Notification__c,
	    					  Event_Notification__c From Chatter_Profile__c where id =: chatterProfileId];
		return cp;	    					  
    }
    
    private boolean isAnniversaryVisible() {
    	boolean visable = true;
    	if(chatterProfile.Relationship_Status__c != null) {    	
	    	if(chatterProfile.Relationship_Status__c == 'Single' 
	    		|| chatterProfile.Relationship_Status__c == 'Widowed') {
	    		visable = false;
	    	}
    	} else {
    		visable = false;
    	}
    	return visable;
    }
    
    public List<EntitySubscriptionWrapper> createEntitySubscriptionWrapper(List<EntitySubscription> followers) {
    	List<EntitySubscriptionWrapper> esWrapper = new List<EntitySubscriptionWrapper>();
    	if(followers != null && !followers.isEmpty()) {
    		Integer count = followers.size();
    		List<EntitySubscription> esList = new List<EntitySubscription>();
    		if(count > MEMBER_PER_ROW) {
    			for(Integer i = 0; i < count; i+4) {
    				EntitySubscription  es1 = followers.get(i);
					EntitySubscription  es2 = (i+1 < count ? followers.get(i+1) : null);
					EntitySubscription  es3 = (i+2 < count ? followers.get(i+2) : null);
					EntitySubscription  es4 = (i+3 < count ? followers.get(i+3) : null);
					esWrapper.add(new EntitySubscriptionWrapper(es1, es2, es3, es4));
    			}
    		} else {
    			Integer i = 0;
    			EntitySubscription  es1 = followers.get(i);
				EntitySubscription  es2 = (i+1 < count ? followers.get(i+1) : null);
				EntitySubscription  es3 = (i+2 < count ? followers.get(i+2) : null);
				EntitySubscription  es4 = (i+3 < count ? followers.get(i+3) : null);
				esWrapper.add(new EntitySubscriptionWrapper(es1, es2, es3, es4));
    		}
    	}
    	return esWrapper;
    }
    
    public class EntitySubscriptionWrapper {
    	public EntitySubscription  es1 {get;set;}
    	public EntitySubscription  es2 {get;set;}
    	public EntitySubscription  es3 {get;set;}
    	public EntitySubscription  es4 {get;set;}
    	
    	public EntitySubscriptionWrapper(EntitySubscription  entity1, EntitySubscription  entity2, 
    							EntitySubscription  entity3, EntitySubscription  entity4) {
			es1 = entity1;
			es2 = entity2;
			es3 = entity3;
			es4 = entity4;    		
    	}
    } 

}